package com.zeng.ssm.service.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis客户端
 *
 * @author zengwanlin
 * @date 2023-09-15T13:42
 */
@Component
public class RedisClient {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private OpsForString opsForStringInstance = null;

    private OpsForHash opsForHashInstance = null;

    private OpsForList opsForListInstance = null;

    private OpsForSet opsForSetInstance = null;

    private OpsForZSet opsForZSetInstance = null;

    public OpsForString opsForString() {
        if (this.opsForStringInstance == null) {
            synchronized (RedisClient.class) {
                if (this.opsForStringInstance == null) {
                    this.opsForStringInstance = new OpsForString(this.redisTemplate);
                }
            }
        }
        return this.opsForStringInstance;
    }

    public OpsForHash opsForHash() {
        if (this.opsForHashInstance == null) {
            synchronized (RedisClient.class) {
                if (this.opsForHashInstance == null) {
                    this.opsForHashInstance = new OpsForHash(this.redisTemplate);
                }
            }
        }
        return this.opsForHashInstance;
    }

    public OpsForList opsForList() {
        if (this.opsForListInstance == null) {
            synchronized (RedisClient.class) {
                if (this.opsForListInstance == null) {
                    this.opsForListInstance = new OpsForList(this.redisTemplate);
                }
            }
        }
        return this.opsForListInstance;
    }

    public OpsForSet opsForSet() {
        if (this.opsForSetInstance == null) {
            synchronized (RedisClient.class) {
                if (this.opsForSetInstance == null) {
                    this.opsForSetInstance = new OpsForSet(this.redisTemplate);
                }
            }
        }
        return this.opsForSetInstance;
    }


    public OpsForZSet opsForZSet() {
        if (this.opsForZSetInstance == null) {
            synchronized (RedisClient.class) {
                if (this.opsForZSetInstance == null) {
                    this.opsForZSetInstance = new OpsForZSet(this.redisTemplate);
                }
            }
        }
        return this.opsForZSetInstance;
    }

    public Boolean delete(String key) {
        return this.redisTemplate.delete(key);
    }

    public Long delete(Collection<String> keys) {
        return this.redisTemplate.delete(keys);
    }

    public Boolean expire(String key, long timeout, TimeUnit timeUnit) {
        return this.redisTemplate.expire(key, timeout, timeUnit);
    }

    public Boolean expireAt(String key, Date date) {
        return this.redisTemplate.expireAt(key, date);
    }

    public Boolean hasKey(String key) {
        return this.redisTemplate.hasKey(key);
    }

    public Boolean persist(String key) {
        return this.redisTemplate.persist(key);
    }

    public void rename(String oldKey, String newKey) {
        this.redisTemplate.rename(oldKey, newKey);
    }

    public void renameIfAbsent(String oldKey, String newKey) {
        this.redisTemplate.renameIfAbsent(oldKey, newKey);
    }

    public Long getExpire(String key) {
        return this.redisTemplate.getExpire(key);
    }

    public Long getExpire(String key, TimeUnit timeUnit) {
        return this.redisTemplate.getExpire(key, timeUnit);
    }

    public <T> T execute(RedisCallback<T> callback) {
        return this.redisTemplate.execute(callback);
    }

    public <T> T execute(SessionCallback<T> callback) {
        return this.redisTemplate.execute(callback);
    }

    public <T> T execute(RedisScript<T> script, List<String> keys, Object... args) {
        return this.redisTemplate.execute(script, keys, args);
    }

    public <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSerializer<T> resultSerializer, List<String> keys, Object... args) {
        return this.redisTemplate.execute(script, argsSerializer, resultSerializer, keys, args);
    }

    /**
     * String类型数据
     */
    public class OpsForString {

        private RedisTemplate<String, Object> redisTemplate;

        public OpsForString(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }

        public void set(String key, Object value) {
            this.redisTemplate.opsForValue().set(key, value);
        }

        public void set(String key, Object value, long timeout, TimeUnit timeUnit) {
            this.redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
        }

        public void set(String key, Object value, Duration duration) {
            this.redisTemplate.opsForValue().set(key, value, duration);
        }

        public Boolean setIfAbsent(String key, Object value) {
            return this.redisTemplate.opsForValue().setIfAbsent(key, value);
        }

        public Boolean setIfAbsent(String key, Object value, long timeout, TimeUnit timeUnit) {
            return this.redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
        }

        public Boolean setIfAbsent(String key, Object value, Duration duration) {
            return this.redisTemplate.opsForValue().setIfAbsent(key, value, duration);
        }

        public Boolean setIfPresent(String key, Object value) {
            return this.redisTemplate.opsForValue().setIfPresent(key, value);
        }

        public Boolean setIfPresent(String key, Object value, long timeout, TimeUnit timeUnit) {
            return this.redisTemplate.opsForValue().setIfPresent(key, value, timeout, timeUnit);
        }

        public Boolean setIfPresent(String key, Object value, Duration duration) {
            return this.redisTemplate.opsForValue().setIfPresent(key, value, duration);
        }

        public Boolean setBit(String key, long offset, boolean value) {
            return this.redisTemplate.opsForValue().setBit(key, offset, value);
        }

        public Object get(String key) {
            return this.redisTemplate.opsForValue().get(key);
        }

        public Object getAndSet(String key, Object newValue) {
            return this.redisTemplate.opsForValue().getAndSet(key, newValue);
        }

        public Boolean getBit(String key, long offset) {
            return this.redisTemplate.opsForValue().getBit(key, offset);
        }

        public Long increment(String key) {
            return this.redisTemplate.opsForValue().increment(key);
        }

        public Long increment(String key, long delta) {
            return this.redisTemplate.opsForValue().increment(key, delta);
        }

        public Double increment(String key, double delta) {
            return this.redisTemplate.opsForValue().increment(key, delta);
        }

        public Long decrement(String key) {
            return this.redisTemplate.opsForValue().decrement(key);
        }

        public Long decrement(String key, long delta) {
            return this.redisTemplate.opsForValue().decrement(key, delta);
        }

        public void multiSet(Map<String, Object> map) {
            this.redisTemplate.opsForValue().multiSet(map);
        }

        public Boolean multiSetIfAbsent(Map<String, Object> map) {
            return this.redisTemplate.opsForValue().multiSetIfAbsent(map);
        }

        public List<Object> multiGet(Collection<String> keys) {
            return this.redisTemplate.opsForValue().multiGet(keys);
        }

        public Long size(String key) {
            return this.redisTemplate.opsForValue().size(key);
        }

    }

    /**
     * Hash类型数据
     */
    public class OpsForHash {

        private RedisTemplate<String, Object> redisTemplate;

        public OpsForHash(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }

        public void put(String key, Object hashKey, Object value) {
            this.redisTemplate.opsForHash().put(key, hashKey, value);
        }

        public void putAll(String key, Map<Object, Object> map) {
            this.redisTemplate.opsForHash().putAll(key, map);
        }

        public void putIfAbsent(String key, Object hashKey, Object value) {
            this.redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
        }

        public Object get(String key, Object hashKey) {
            return this.redisTemplate.opsForHash().get(key, hashKey);
        }

        public List<Object> multiGet(String key, Collection<Object> hashKeys) {
            return this.redisTemplate.opsForHash().multiGet(key, hashKeys);
        }

        public List<Object> values(String key) {
            return this.redisTemplate.opsForHash().values(key);
        }

        public Long delete(String key, Object... hashKeys) {
            return this.redisTemplate.opsForHash().delete(key, hashKeys);
        }

        public Long increment(String key, Object hashKey, long delta) {
            return this.redisTemplate.opsForHash().increment(key, hashKey, delta);
        }

        public Double increment(String key, Object hashKey, double delta) {
            return this.redisTemplate.opsForHash().increment(key, hashKey, delta);
        }

        public Map<Object, Object> entries(String key) {
            return this.redisTemplate.opsForHash().entries(key);
        }

        public Boolean hasKey(String key, Object hashKey) {
            return this.redisTemplate.opsForHash().hasKey(key, hashKey);
        }

        public Set<Object> keys(String key) {
            return this.redisTemplate.opsForHash().keys(key);
        }

        public Long size(String key) {
            return this.redisTemplate.opsForHash().size(key);
        }

    }


    /**
     * List类型数据
     */
    public class OpsForList {

        private RedisTemplate<String, Object> redisTemplate;

        public OpsForList(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }

        public Long leftPush(String key, Object value) {
            return this.redisTemplate.opsForList().leftPush(key, value);
        }

        public Long leftPushAll(String key, Object... values) {
            return this.redisTemplate.opsForList().leftPushAll(key, values);
        }

        public Long leftPushAll(String key, Collection<Object> values) {
            return this.redisTemplate.opsForList().leftPushAll(key, values);
        }

        public Long leftPushIfPresent(String key, Object value) {
            return this.redisTemplate.opsForList().leftPushIfPresent(key, value);
        }

        public Long rightPush(String key, Object value) {
            return this.redisTemplate.opsForList().rightPush(key, value);
        }

        public Long rightPushAll(String key, Object... values) {
            return this.redisTemplate.opsForList().rightPushAll(key, values);
        }

        public Long rightPushAll(String key, Collection<Object> values) {
            return this.redisTemplate.opsForList().rightPushAll(key, values);
        }

        public Long rightPushIfPresent(String key, Object value) {
            return this.redisTemplate.opsForList().rightPushIfPresent(key, value);
        }

        public Object leftPop(String key) {
            return this.redisTemplate.opsForList().leftPop(key);
        }

        public Object leftPop(String key, long timeout, TimeUnit timeUnit) {
            return this.redisTemplate.opsForList().leftPop(key, timeout, timeUnit);
        }

        public Object rightPop(String key) {
            return this.redisTemplate.opsForList().rightPop(key);
        }

        public Object rightPop(String key, long timeout, TimeUnit timeUnit) {
            return this.redisTemplate.opsForList().rightPop(key, timeout, timeUnit);
        }

        public Object rightPopAndLeftPush(String sourceKey, String destinationKey) {
            return this.redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
        }

        public Object rightPopAndLeftPush(String sourceKey, String destinationKey, long timeout, TimeUnit timeUnit) {
            return this.redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, timeUnit);
        }

        public Object index(String key, long index) {
            return this.redisTemplate.opsForList().index(key, index);
        }

        public List<Object> range(String key, long start, long end) {
            return this.redisTemplate.opsForList().range(key, start, end);
        }

        public Long remove(String key, long count, Object value) {
            return this.redisTemplate.opsForList().remove(key, count, value);
        }

        public Long size(String key) {
            return this.redisTemplate.opsForList().size(key);
        }

        public void trim(String key, long start, long end) {
            this.redisTemplate.opsForList().trim(key, start, end);
        }

    }

    /**
     * Set类型数据
     */
    public class OpsForSet {

        private RedisTemplate<String, Object> redisTemplate;

        public OpsForSet(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }

        public Long add(String key, Object... values) {
            return this.redisTemplate.opsForSet().add(key, values);
        }

        public Long remove(String key, Object... values) {
            return this.redisTemplate.opsForSet().remove(key, values);
        }

        public Set<Object> members(String key) {
            return this.redisTemplate.opsForSet().members(key);
        }

        public Set<Object> difference(String key, String otherKey) {
            return this.redisTemplate.opsForSet().difference(key, otherKey);
        }

        public Set<Object> difference(String key, Collection<String> otherKeys) {
            return this.redisTemplate.opsForSet().difference(key, otherKeys);
        }

        public Long differenceAndStore(String key, String otherKey, String destKey) {
            return this.redisTemplate.opsForSet().differenceAndStore(key, otherKey, destKey);
        }

        public Long differenceAndStore(String key, Collection<String> otherKeys, String destKey) {
            return this.redisTemplate.opsForSet().differenceAndStore(key, otherKeys, destKey);
        }

        public Set<Object> intersect(String key, String otherKey) {
            return this.redisTemplate.opsForSet().intersect(key, otherKey);
        }

        public Set<Object> intersect(String key, Collection<String> otherKeys) {
            return this.redisTemplate.opsForSet().intersect(key, otherKeys);
        }

        public Long intersectAndStore(String key, String otherKey, String destKey) {
            return this.redisTemplate.opsForSet().intersectAndStore(key, otherKey, destKey);
        }

        public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey) {
            return this.redisTemplate.opsForSet().intersectAndStore(key, otherKeys, destKey);
        }

        public Set<Object> distinctRandomMembers(String key, long count) {
            return this.redisTemplate.opsForSet().distinctRandomMembers(key, count);
        }

        public Boolean isMember(String key, Object value) {
            return this.redisTemplate.opsForSet().isMember(key, value);
        }

        public Boolean isMember(String key, Object value, String destKey) {
            return this.redisTemplate.opsForSet().move(key, value, destKey);
        }

        public Object pop(String key) {
            return this.redisTemplate.opsForSet().pop(key);
        }

        public List<Object> pop(String key, long count) {
            return this.redisTemplate.opsForSet().pop(key, count);
        }

        public Set<Object> union(String key, String otherKey) {
            return this.redisTemplate.opsForSet().union(key, otherKey);
        }

        public Set<Object> union(String key, Collection<String> otherKeys) {
            return this.redisTemplate.opsForSet().union(key, otherKeys);
        }

        public Long unionAndStore(String key, String otherKey, String destKey) {
            return this.redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
        }

        public Long unionAndStore(String key, Collection<String> otherKeys, String destKey) {
            return this.redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
        }

        public Object randomMember(String key) {
            return this.redisTemplate.opsForSet().randomMember(key);
        }

        public List<Object> randomMembers(String key, long count) {
            return this.redisTemplate.opsForSet().randomMembers(key, count);
        }

        public Long size(String key) {
            return this.redisTemplate.opsForSet().size(key);
        }

    }

    /**
     * ZSet类型数据
     */
    public class OpsForZSet {

        private RedisTemplate<String, Object> redisTemplate;

        public OpsForZSet(RedisTemplate<String, Object> redisTemplate) {
            this.redisTemplate = redisTemplate;
        }

        public Boolean add(String key, Object value, double score) {
            return this.redisTemplate.opsForZSet().add(key, value, score);
        }

        public Long add(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
            return this.redisTemplate.opsForZSet().add(key, tuples);
        }

        public Long remove(String key, Object... values) {
            return this.redisTemplate.opsForZSet().remove(key, values);
        }

        public Double incrementScore(String key, Object value, double delta) {
            return this.redisTemplate.opsForZSet().incrementScore(key, value, delta);
        }

        public Long rank(String key, Object value) {
            return this.redisTemplate.opsForZSet().rank(key, value);
        }

        public Long reverseRank(String key, Object value) {
            return this.redisTemplate.opsForZSet().reverseRank(key, value);
        }

        public Set<Object> range(String key, long start, long end) {
            return this.redisTemplate.opsForZSet().range(key, start, end);
        }

        public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, long start, long end) {
            return this.redisTemplate.opsForZSet().rangeWithScores(key, start, end);
        }

        public Set<Object> rangeByScore(String key, double min, double max) {
            return this.redisTemplate.opsForZSet().rangeByScore(key, min, max);
        }

        public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max) {
            return this.redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
        }

        public Set<Object> rangeByScore(String key, double min, double max, long offset, long count) {
            return this.redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
        }

        public Set<ZSetOperations.TypedTuple<Object>> rangeByScoreWithScores(String key, double min, double max, long offset, long count) {
            return this.redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max, offset, count);
        }

        public Set<Object> reverseRange(String key, long start, long end) {
            return this.redisTemplate.opsForZSet().reverseRange(key, start, end);
        }

        public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end) {
            return this.redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
        }

        public Set<Object> reverseRangeByScore(String key, double min, double max) {
            return this.redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
        }

        public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max) {
            return this.redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
        }

        public Set<Object> reverseRangeByScore(String key, double min, double max, long offset, long count) {
            return this.redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, offset, count);
        }

        public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max, long offset, long count) {
            return this.redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max, offset, count);
        }

        public Long count(String key, double min, double max) {
            return this.redisTemplate.opsForZSet().count(key, min, max);
        }

        public Long size(String key) {
            return this.redisTemplate.opsForZSet().size(key);
        }

        public Long zCard(String key) {
            return this.redisTemplate.opsForZSet().zCard(key);
        }

        public Double score(String key, Object value) {
            return this.redisTemplate.opsForZSet().score(key, value);
        }

        public Long removeRange(String key, long start, long end) {
            return this.redisTemplate.opsForZSet().removeRange(key, start, end);
        }

        public Long removeRangeByScore(String key, double min, double max) {
            return this.redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
        }

        public Long unionAndStore(String key, String otherKey, String destKey) {
            return this.redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
        }

        public Long unionAndStore(String key, Collection<String> otherKeys, String destKey) {
            return this.redisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey);
        }

        public Long unionAndStore(String key, Collection<String> otherKeys, String destKey, RedisZSetCommands.Aggregate aggregate) {
            return this.redisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey, aggregate);
        }

        public Long unionAndStore(String key, Collection<String> otherKeys, String destKey, RedisZSetCommands.Aggregate aggregate, RedisZSetCommands.Weights weights) {
            return this.redisTemplate.opsForZSet().unionAndStore(key, otherKeys, destKey, aggregate, weights);
        }

        public Long intersectAndStore(String key, String otherKey, String destKey) {
            return this.redisTemplate.opsForZSet().intersectAndStore(key, otherKey, destKey);
        }

        public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey) {
            return this.redisTemplate.opsForZSet().intersectAndStore(key, otherKeys, destKey);
        }

        public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey, RedisZSetCommands.Aggregate aggregate) {
            return this.redisTemplate.opsForZSet().intersectAndStore(key, otherKeys, destKey, aggregate);
        }

        public Long intersectAndStore(String key, Collection<String> otherKeys, String destKey, RedisZSetCommands.Aggregate aggregate, RedisZSetCommands.Weights weights) {
            return this.redisTemplate.opsForZSet().intersectAndStore(key, otherKeys, destKey, aggregate, weights);
        }

        public Set<Object> rangeByLex(String key, RedisZSetCommands.Range range) {
            return this.redisTemplate.opsForZSet().rangeByLex(key, range);
        }

        public Set<Object> rangeByLex(String key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit) {
            return this.redisTemplate.opsForZSet().rangeByLex(key, range, limit);
        }

    }

}
